<?php

namespace App\Models\goods;

use App\Models\base\traits\ImgTrait;
use Encore\Admin\Traits\AdminBuilder;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\ModelTree;


/**
 * App\Models\goods\GoodsCate
 *
 * @property int $id
 * @property string $name 商品分类名
 * @property int $pid 父ID
 * @property string $icon icon
 * @property string|null $img 推广图
 * @property int $is_top 是否置顶(显示在首页)
 * @property int $sort 排序
 * @property int $status
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\goods\GoodsCate $cate
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\goods\GoodsCate[] $children
 * @property-read int|null $children_count
 * @property-read mixed $header_img
 * @property mixed $icons
 * @property mixed $imgs
 * @property mixed $parameter
 * @property-read \App\Models\goods\GoodsCate $parent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\goods\GoodsCate[] $subs
 * @property-read int|null $subs_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereIcon($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereIsTop($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\GoodsCate whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class GoodsCate extends Model
{
    use ImgTrait , ModelTree, AdminBuilder;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setParentColumn('pid');
        $this->setOrderColumn('sort');
        $this->setTitleColumn('name');
    }

    protected $fillable = [
        'name',
        'pid',
        'icon',
        'img',
        'is_top',
        'sort',
    ];

    public function cate()
    {
        return $this->hasOne(GoodsCate::class, 'id','pid');
    }

    public function subs()
    {
        return $this->hasMany(GoodsCate::class,'pid')
            ->select(['id','icon','name','pid'])
            ->orderBy('sort');
    }

}
